/*
 * $Id: PropertiesUtil.java 9 2006-03-08 10:21:59Z wjx $
 */
package com.someok.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;


/**
 * Misc java.util.Properties utils.
 *
 * <p><a href="PropertiesUtil.java.html"><i>View Source</i></a></p>
 *
 * @version $Revision: 9 $ $Date: 2006-03-08 18:21:59 +0800 (星期三, 08 三月 2006) $
 */
public final class PropertiesUtil {

	// ---------------------------------------------------------------- to/from files

	/**
	 * Create properties from the file.
	 *
	 * @param fileName properties file name to load
	 *
	 * @exception IOException
	 */
	public static Properties createFromFile(String fileName) throws IOException {
		return createFromFile(new File(fileName));
	}

	/**
	 * Create properties from the file.
	 *
	 * @param file properties file to load
	 *
	 * @exception IOException
	 */
	public static Properties createFromFile(File file) throws IOException {
		Properties prop = new Properties();
		loadFromFile(prop, file);
		return prop;
	}

	/**
	 * Loads properties from the file. Properties are appended to the existing
	 * properties object.
	 *
	 * @param p        properties to fill in
	 * @param fileName properties file name to load
	 *
	 * @exception IOException
	 */
	public static void loadFromFile(Properties p, String fileName) throws IOException {
		loadFromFile(p, new File(fileName));
	}

	/**
	 * Loads properties from the file. Properties are appended to the existing
	 * properties object.
	 *
	 * @param p      properties to fill in
	 * @param file   file to read properties from
	 *
	 * @exception IOException
	 */
	public static void loadFromFile(Properties p, File file) throws IOException {
		if (p == null) {
			return;
		}
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			p.load(fis);
		} finally {
			if (fis != null) {
				fis.close();
			}
		}
	}


	/**
	 * Writes properties to a file.
	 *
	 * @param p        properties to write to file
	 * @param fileName destination file name
	 *
	 * @exception IOException
	 */
	public static void writeToFile(Properties p, String fileName) throws IOException {
		writeToFile(p, new File(fileName), null);
	}

	/**
	 * Writes properties to a file.
	 *
	 * @param p        properties to write to file
	 * @param fileName destiantion file name
	 * @param header   optional header
	 *
	 * @exception IOException
	 */
	public static void writeToFile(Properties p, String fileName, String header) throws IOException {
		writeToFile(p, new File(fileName), header);
	}

	/**
	 * Writes properties to a file.
	 *
	 * @param p      properties to write to file
	 * @param file   destination file
	 *
	 * @exception IOException
	 */
	public static void writeToFile(Properties p, File file) throws IOException {
		writeToFile(p, file, null);
	}

	/**
	 * Writes properties to a file.
	 *
	 * @param p      properties to write to file
	 * @param file   destiantion file
	 * @param header optional header
	 *
	 * @exception IOException
	 */
	public static void writeToFile(Properties p, File file, String header) throws IOException {
		if (p == null) {
			return;
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			p.store(fos, header);
		} finally {
			if (fos != null) {
				fos.close();
			}
		}
	}


	// ---------------------------------------------------------------- properties

	/**
	 * Creates new Properties object from the original one, only by copying
	 * those properties that have specific first part of the key name.
	 *
	 * @param p      Source properties, from which new object will be created
	 * @param base   Base properties key names.
	 *
	 * @return subset properties
	 */
	public static Properties getSubset(Properties p, String base) {
		if (p == null) {
			return null;
		}
		if (StringUtil.isEmpty(base) == true) {
			return p;
		}
		base += ".";
		Properties res = new Properties();
		Iterator iti = p.keySet().iterator();
		int baseLen = base.length();
		while (iti.hasNext()) {
			String key = (String)iti.next();
			if (key.startsWith(base) == true) {
				res.put(key.substring(baseLen), p.get(key));
			}
		}
		return res;
	}

}
